# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html
import codecs
import MySQLdb
from scrapy import log
import MySQLdb.cursors
from twisted.enterprise import adbapi

class TutorialPipeline(object):
    def open_spider(self,spider):
        print "begin........"
        self.file = codecs.open('joke.txt','w',encoding="utf-8")
    def close_spider(self,spider):
        print "end......"
        self.file.close()
    def process_item(self, item, spider):
        # return item
        self.file.write(item['content'])
        self.file.write("\n\n")
class TutorialMySQLPipeline(object):
    """docstring for MySQLPipeline"""
    def __init__(self):
        self.connpool = adbapi.ConnectionPool('MySQLdb',
            #host = '127.0.0.1', #localhost和127.0.0.1 都代表本地主机
            host = 'localhost',
            db = 'baike',
            user = 'root',
            passwd = '',
            cursorclass = MySQLdb.cursors.DictCursor,
            charset = 'utf8',
            use_unicode = True
            )
    def process_item(self, item, spider):
        query = self.connpool.runInteraction(self._conditional_insert, item)
        query.addErrback(self.handle_error)
        return item

    def _conditional_insert(self, tx, item):
        if item.get('content'):
            tx.execute("insert into jokji (content) values('%s')"%item['content'])
    def handle_error(self, e):
        log.err(e)